package com.vehicle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vehicle.bean.Area;
import com.vehicle.bean.ParkingLot;
import com.vehicle.form.AreaAddForm;
import com.vehicle.mapper.AreaMapper;
import com.vehicle.mapper.CategoryMapper;
import com.vehicle.mapper.ParkingLotMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vehicle.mapper.UserLogMapper;
import com.vehicle.service.AreaService;
import com.vehicle.util.LogUtil;
import com.vehicle.vo.AreaVO;
import com.vehicle.vo.FilterVO;
import com.vehicle.vo.ListAreaVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 校园车辆管理系统
 * @since 2023-04-21
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {

    @Autowired
    AreaMapper areaMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    ParkingLotMapper parkingLotMapper;

    @Autowired
    UserLogMapper userLogMapper;

    //获取信息：先在表里面添加一条信息，然后根据parkingNumber和freeNumber在parkingLot表里面添加信息
    @Override
    public AreaVO addAreaMessage(String ip,Integer aid,AreaAddForm areaAddForm,Integer id) {
        Area area = new Area();
        area.setFreeNumber(areaAddForm.getParkingNumber());
        BeanUtils.copyProperties(areaAddForm,area);
        area.setCategoryId(id);
        int b = areaMapper.insert(area);
        QueryWrapper<Area> areaQueryWrapper = new QueryWrapper<>();
        areaQueryWrapper.eq("area_id",area.getAreaId());
        Area area1 = areaMapper.selectOne(areaQueryWrapper);
        if(b==1){
            for (int i = 0; i < area.getParkingNumber(); i++) {
                ParkingLot parkingLot = new ParkingLot();
                parkingLot.setAreaId(area1.getAreaId());
                parkingLot.setSeatState(0);
                parkingLotMapper.insert(parkingLot);
            }
            AreaVO areaVO = new AreaVO();
            areaVO.setAreaId(area1.getAreaId());
            BeanUtils.copyProperties(areaAddForm,areaVO);
            userLogMapper.insert(LogUtil.insertAdminInsert(aid,"添加区域ID["+area1.getAreaId()+"]成功",ip,"成功"));
            return areaVO;
        }
        userLogMapper.insert(LogUtil.insertAdminInsert(aid,"添加区域ID["+area1.getAreaId()+"]失败",ip,"失败"));
        return null;
    }

    //查询所有的停车区域
    @Override
    public ListAreaVO getAllAreaList(Integer page, Integer size) {
        Page<Area> infoPage = new Page<>(page,size);
        Page<Area> areaInfoPage = areaMapper.selectPage(infoPage,null);
        ListAreaVO listAreaVO = new ListAreaVO();
        listAreaVO.setSize(areaInfoPage.getSize());
        listAreaVO.setTotal(areaInfoPage.getTotal());
        List<AreaVO> areaVOS = new ArrayList<>();
        List<Area> areas = areaInfoPage.getRecords();
        System.out.println("arealist为下面"+areas);
        for (Area area:areas) {
            AreaVO areaVO = new AreaVO();
            BeanUtils.copyProperties(area,areaVO);
            System.out.println(areaVO);
            areaVO.setCategoryName(categoryMapper.getCategoryNameById(area.getCategoryId()));
            areaVOS.add(areaVO);
            System.out.println("最后的areaVO为："+areaVO);
        }
        listAreaVO.setContent(areaVOS);
        System.out.println("最最最最终得areaVOS为："+areaVOS);
        return listAreaVO;
    }

    @Override
    public List<AreaVO> getAreaListByContent(String content) {
        QueryWrapper<Area> areaQueryWrapper = new QueryWrapper<>();
        areaQueryWrapper.like("location",content);
        List<Area> areas = areaMapper.selectList(areaQueryWrapper);
        List<AreaVO> areaVOS = new ArrayList<>();
        for (Area area:areas) {
            AreaVO areaVO = new AreaVO();
            BeanUtils.copyProperties(area,areaVO);
            System.out.println(areaVO);
            areaVO.setCategoryName(categoryMapper.getCategoryNameById(area.getCategoryId()));
            areaVOS.add(areaVO);
            System.out.println("最后的areaVO为："+areaVO);
        }
        return areaVOS;
    }

    @Override
    public List<FilterVO> getAreaNameList() {
        List<String> list = areaMapper.getAllAreaName();
        List<FilterVO> filterVOS = new ArrayList<>();
        for (String ss:list) {
            FilterVO filterVO = new FilterVO();
            filterVO.setText(ss);
            filterVO.setValue(ss);
            filterVOS.add(filterVO);
        }
        return filterVOS;
    }

    @Override
    public List<FilterVO> getLocationListByName(String name) {
        List<String> list = areaMapper.getLocationListByName(name);
        List<FilterVO> filterVOS = new ArrayList<>();
        for (String ss:list) {
            FilterVO filterVO = new FilterVO();
            filterVO.setText(ss);
            filterVO.setValue(ss);
            filterVOS.add(filterVO);
        }
        return filterVOS;
    }

    @Override
    public List<FilterVO> getCategoryNameListByNameAndLocation(String name, String location) {
        List<Integer> list = areaMapper.getCategoryIdByNameAndLocation(name,location);
        List<FilterVO> filterVOS = new ArrayList<>();
        for (Integer in:list) {
            String ss = categoryMapper.getCategoryNameById(in);
            FilterVO filterVO = new FilterVO();
            filterVO.setText(ss);
            filterVO.setValue(ss);
            filterVOS.add(filterVO);
        }
        return filterVOS;
    }

    @Override
    public List<FilterVO> getParkingIdByAreaId(String name, String location, String categoryName) {
        Integer id = categoryMapper.getCategoryIdByName(categoryName);
        Integer areaId = areaMapper.getAreaId(name,location,id);
        List<Integer> parkingId = parkingLotMapper.getParkingId(areaId);
        List<FilterVO> filterVOS = new ArrayList<>();
        for (Integer in:parkingId) {
            FilterVO filterVO = new FilterVO();
            filterVO.setText(in.toString());
            filterVO.setValue(in.toString());
            filterVOS.add(filterVO);
        }
        return filterVOS;
    }

    //根据id删除区域
    @Override
    public boolean deleteById(String ip,Integer aid,Integer id) {
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("area_id",id);
        int t = areaMapper.deleteById(id);
//        删除与areaid相关的parkinglot
        QueryWrapper<ParkingLot> wrapper = new QueryWrapper<>();
        wrapper.eq("area_id",id);
        List<ParkingLot> parkingLots = parkingLotMapper.selectList(wrapper);
        for (ParkingLot p:parkingLots) {
            parkingLotMapper.deleteById(p.getParkingId());
        }
        userLogMapper.insert(LogUtil.insertAdminDelete(aid,"删除区域ID["+id+"]成功",ip,"成功"));
        return true;
    }

    @Override
    public boolean updateAreaMessage(String ip,Integer aid,AreaAddForm areaAddForm,Integer id) {
        Area area = areaMapper.selectById(areaAddForm.getAreaId());
        UpdateWrapper<Area> areaUpdateWrapper = new UpdateWrapper<>();
        areaUpdateWrapper.eq("area_id",areaAddForm.getAreaId());
        areaUpdateWrapper.set("name",areaAddForm.getName());
        areaUpdateWrapper.set("location",areaAddForm.getLocation());
        areaUpdateWrapper.set("category_id",id);
        areaUpdateWrapper.set("parking_number",areaAddForm.getParkingNumber());
        int b = areaMapper.update(null,areaUpdateWrapper);
        int cha = areaAddForm.getParkingNumber() - area.getFreeNumber();
        if((b ==1)&&(cha>=0)){
            int more = areaAddForm.getParkingNumber() - area.getParkingNumber();
            if(more>0){
                for (int i = 0; i < more; i++) {
                    ParkingLot parkingLot = new ParkingLot();
                    parkingLot.setSeatState(0);
                    parkingLot.setAreaId(area.getAreaId());
                    parkingLotMapper.insert(parkingLot);
                }
                userLogMapper.insert(LogUtil.insertAdminUpdate(aid,"修改区域ID["+areaAddForm.getAreaId()+"]成功",ip,"成功"));
                return true;
            }
            if(more<0){
                int more2 = area.getParkingNumber() - areaAddForm.getParkingNumber();
                QueryWrapper<ParkingLot> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("area_id",area.getAreaId());
                queryWrapper.eq("parking_state",0);
                List<ParkingLot> parkingLots = parkingLotMapper.selectList(queryWrapper);
                for (ParkingLot p2:parkingLots) {
                    parkingLotMapper.deleteById(p2.getParkingId());
                }
                for (int i = 0; i < more2; i++) {
                    ParkingLot parkingLot = new ParkingLot();
                    parkingLot.setSeatState(0);
                    parkingLot.setAreaId(area.getAreaId());
                    parkingLotMapper.insert(parkingLot);
                }
                userLogMapper.insert(LogUtil.insertAdminUpdate(aid,"修改区域ID["+areaAddForm.getAreaId()+"]成功",ip,"成功"));
                return true;
            }
            if(more==0){
                userLogMapper.insert(LogUtil.insertAdminUpdate(aid,"修改区域ID["+areaAddForm.getAreaId()+"]成功",ip,"成功"));
                return true;
            }
        }
        userLogMapper.insert(LogUtil.insertAdminUpdate(aid,"修改区域ID["+areaAddForm.getAreaId()+"]失败",ip,"失败"));
        return false;
    }
}
